<?php

namespace app\supplier\controller;

use app\erp\model\Staffdb;
use app\erp\model\Departmentdb;

class Supplier extends Base {
	protected $cols = array (
			array (
					'field' => 'member_id',
					'width' => 40,
					'title' => 'ID',
					'type' => 'checkbox',
					'fixed' => 'left' 
			),
			array (
					'field' => 'supplier_name',
					'width' => 260,
					'title' => '供应商',
					'sort' => 'true' 
			),
			array (
					'field' => 'username',
					'width' => 160,
					'title' => '登录名',
					'sort' => 'true' 
			),
			array (
					'field' => 'realname',
					'width' => 160,
					'title' => '联系人',
					'sort' => 'true'
			),
			array (
					'field' => 'phone',
					'width' => 160,
					'title' => '联系电话',
					'sort' => 'true'
			),
			array (
					'field' => 'supplier_rate',
					'width' => 160,
					'title' => '折扣率%'
			),
			array (
					'field' => 'create_time',
					'width' => 120,
					'title' => '录入时间',
					'sort' => 'true' 
			),
			array (
					'fixed' => 'right',
					'width' => 120,
					'toolbar' => '#supplier_tool'
			)
	);
	public $post;
	public function _initialize() {
		parent::_initialize ();
		$request = request ();
		$param = $request->param ();
		$this->post = $param;
		if (isset ( $this->post ['staff_id'] )) {
			$this->post ['member_id'] = $this->post ['staff_id'];
			unset ( $this->post ['staff_id'] );
		}
		if (isset ( $this->post ['_o'] )) {
			$this->post ['_o'] = str_replace ( '_text', '', $this->post ['_o'] );
		}
	}
	public function index() {
		$this->assign ( 'cols', json_encode ( $this->cols ) );
		return $this->fetch ();
	}
	public function ajaxList() {
		$staffdb = new Staffdb ();
		$this->post ['is_staff'] = 'yes';
		$staffdb->param = $this->post;
		$staffdb->param ['is_supplier'] = 1;
		$staffdb->param['site_id'] = 117;
		$return = $staffdb->ajaxSelect ();
		$this->layuiReturn ( $return ['data'], $return ['count'] );
	}
	public function ajaxMember() {
		$staffdb = new Staffdb ();
		$staffdb->param = $this->post;
		$return = $staffdb->ajaxSelect ();
		$this->layuiReturn ( $return ['data'], $return ['count'] );
	}
	public function tplForm() {
		$member_id = '';
		$data = array ();
		if (isset ( $this->post ['member_id'] ) && ! empty ( $this->post ['member_id'] )) {
			// 获取数据
			$staffdb = new Staffdb ();
			$staffdb->param = $this->post;
			$data = $staffdb->ajaxFind ();
			unset ( $data ['password'] );
			$member_id = $this->post ['member_id'];
		}
		$this->assign ( 'data', json_encode ( $data ) );
		$this->assign ( 'staff_id', $member_id );
		return $this->fetch ();
	}
	/**
	 *
	 * @todo 保存数据
	 */
	public function saveData() {
		$staffdb = new Staffdb ();
		$staffdb->param = $this->post;
		$staffdb->param ['is_manage'] = 2;
		$return = $staffdb->ajaxSave ();
		if ($return != 'success') {
			$this->layuiReturn ( $return, 0, $return, - 1 );
		} else {
			$this->layuiReturn ( '保存成功' );
		}
	}
	/**
	 *
	 * @todo 获取单条数据
	 */
	public function ajaxOneData() {
		$staffdb = new Staffdb ();
		$staffdb->param = $this->post;
		$data = $staffdb->ajaxFind ();
		$this->layuiReturn ( $data, 1 );
	}
	/**
	 *
	 * @todo 软删除
	 */
	public function ajaxDelete() {
		$staff = new Staffdb ();
		$staff->param = $this->post;
		$staff->ajaxDelete ();
		$this->layuiReturn ( '删除成功', 0 );
	}
	public function ajaxRestore() {
		$staff = new Staffdb ();
		$staff->param = $this->post;
		$staff->ajaxRestore ();
		$this->layuiReturn ( '数据恢复成功', 0 );
	}
	// 回收站
	public function recycle() {
		array_push ( $this->cols, array (
				'field' => 'delete_time',
				'width' => 120,
				'title' => '删除时间' 
		), array (
				'fixed' => 'right',
				'width' => 80,
				'toolbar' => '#recycle_staff_tool' 
		) );
		$this->assign ( 'cols', json_encode ( $this->cols ) );
		return $this->fetch ();
	}
	public function getCityDepartment() {
		$departmentdb = new Departmentdb ();
		
		$param = array (
				'site_id' => $this->post ['site_id'],
				'_o' => 'department_id',
				'_b' => 'desc',
				'page' => 1,
				'limit' => 30 
		);
		$departmentdb->param = $param;
		$data = $departmentdb->ajaxSelect ();
		$this->layuiReturn ( $data ['data'], $data ['count'] );
	}
}